#include "VSDx9Renderer.h"
#include "VS2DTexSampler.h"
#include "VSGeometry.h"
#include "VSString.h"
#include "VSResourceManager.h"
#include "VSShaderStringFactory.h"
#include "VSGraphicInclude.h"
#include "VSOrenNayarShaderFunction.h"
using namespace VSEngine2;
VSString VSDX9Renderer::Tex2D(const VS2DTexSampler * p2DTexSampler) const
{
	VSMAC_ASSERT(p2DTexSampler);
	if (!p2DTexSampler)
		return VSString();
	return p2DTexSampler->GetOutputNode(VS2DTexSampler::OUT_COLOR)->GetNodeName().GetString()
			+ _T(" = tex2D(") + p2DTexSampler->GetShowName().GetString() + _T(",") + 
			p2DTexSampler->GetInputNode(VS2DTexSampler::IN_TEXCOORD)->GetNodeName().GetString() + _T(");\n");
}
VSString VSDX9Renderer::Sampler(unsigned int uiType)const
{
	if (uiType == VSTexture::TT_1D)
	{
		return VSString(_T("sampler1D "));
	}
	else if (uiType == VSTexture::TT_2D)
	{
		return VSString(_T("sampler2D "));
	}
	else if (uiType == VSTexture::TT_3D)
	{
		return VSString(_T("sampler3D "));
	}
	else if (uiType == VSTexture::TT_CUBE)
	{
		return VSString(_T("samplerCUBE "));
	}
	return VSString::ms_StringNULL;
}
VSString VSDX9Renderer::Float()const
{
	return VSString(_T("float "));
}
VSString VSDX9Renderer::Float2()const
{
	return VSString(_T("float2 "));
}
VSString VSDX9Renderer::Float3()const
{
	return VSString(_T("float3 "));
}
VSString VSDX9Renderer::Float4()const
{
	return VSString(_T("float4 "));
}

VSString VSDX9Renderer::FloatConst(const VSString & Value1)const
{
	return Value1;
}
VSString VSDX9Renderer::Float2Const(const VSString &Value1,const VSString &Value2)const
{
	return VSString(_T("float2")) + _T("(") + Value1 + _T(",") + Value2 + _T(")");
}
VSString VSDX9Renderer::Float3Const(const VSString & Value1,
							 const VSString& Value2,const VSString &Value3)const
{
	return VSString(_T("float3")) + _T("(") + Value1 + _T(",") + Value2 + _T(",") + Value3 + _T(")");
}
VSString VSDX9Renderer::Float4Const(const VSString &Value1,const VSString &Value2,
							 const VSString &Value3,const VSString &Value4)const
{
	return VSString(_T("float4")) + _T("(") + Value1 + _T(",") + Value2 + _T(",") + Value3 + _T(",") + Value4 + _T(")");
}
VSString VSDX9Renderer::GetValueElement(const VSPutNode * pPutNode,
								 unsigned char uiVE)const
{
	VSMAC_ASSERT(pPutNode);
	if (!pPutNode)
		return VSString::ms_StringNULL;
	VSString Temp = pPutNode->GetNodeName().GetString();
	if (uiVE > 0)
	{
		if (pPutNode->GetValueType() == VSPutNode::VT_1 && (uiVE & VE_R))
		{
			return Temp;
		}
		else if(pPutNode->GetValueType() == VSPutNode::VT_1)
		{
			VSMAC_ASSERT(0);
			return VSString::ms_StringNULL;
		}
		VSString Value[4];
		Value[0] = _T("r");
		Value[1] = _T("g");
		Value[2] = _T("b");
		Value[3] = _T("a");
		unsigned int Mask[4];
		Mask[0] = VE_R;
		Mask[1] = VE_G;
		Mask[2] = VE_B;
		Mask[3] = VE_A;
		Temp +=  _T(".");
		
		for (unsigned int i = 0 ; i < 4 ; i++)
		{
			if ( i <= pPutNode->GetValueType())
			{
				if (uiVE & Mask[i])
				{
					Temp += Value[i];
				}
			}
			
		}

	}

	
	return Temp;
}
VSString VSDX9Renderer::SetRegister(unsigned int uiRegisterType,unsigned int uiRegisterIndex)const
{

	VSString RegisterID = IntToString(uiRegisterIndex);
	if(uiRegisterType == RT_B)
		return _T(" : register(b") + RegisterID + _T(")");
	else if(uiRegisterType == RT_C)
		return _T(" : register(c") + RegisterID + _T(")");
	if(uiRegisterType == RT_S)
		return _T(" : register(s") + RegisterID + _T(")");
	else
		VSMAC_ASSERT(1);
	return VSString();
}
void VSDX9Renderer::CreateVInputDeclare(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		for(unsigned int i = 0 ; i < pVBuffer->GetPositionLevel(); i++)
		{	
			if(pVBuffer->HavePositionInfo(i))
			{
				VSString VertexID = IntToString(i);		
				TempDeclare += _T("float3 Position") + VertexID + _T(":POSITION") + VertexID + _T(";\n");
			}

		}

		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{
			if(pVBuffer->HaveTexCoordInfo(i))
			{
				VSString TextureID = IntToString(i);
				TempDeclare += _T("float2 TexCoord") + TextureID+ _T(":TEXCOORD") + TextureID + _T(";\n");
			}
		}

		unsigned int uiLightNum = MSPara1.LightArray.GetNum();
		if(uiLightNum)
		{

			for(unsigned int i = 0 ; i < pVBuffer->GetNormalLevel(); i++)
			{
				if(pVBuffer->HaveNormalInfo(i))
				{
					VSString NomalID = IntToString(i);
					TempDeclare += _T("float3 Normal") + NomalID + _T(":NORMAL") + NomalID + _T(";\n");
				}
			}

			if(pVBuffer->HaveTangentInfo())
			{
				TempDeclare += _T("float4 Tangent : TANGENT;\n");
				if (pVBuffer->HaveBinormalInfo())
				{
					TempDeclare += _T("float3 Binormal : BINORMAL;\n");
				}
			}


		}

		if(pVBuffer->HavePSizeInfo())
		{
			TempDeclare += _T("float PSize : PSIZE;\n");
		}

		for(unsigned int i = 0 ; i < pVBuffer->GetColorLevel(); i++)
		{

			if(pVBuffer->HaveColorInfo(0))
			{
				VSString ColorID = IntToString(i);
				TempDeclare += _T("float4 Color") + ColorID + _T(":COLOR") + ColorID + _T(";\n");
			}
		}

		if(pVBuffer->HaveBlendWeightInfo())
		{
			TempDeclare += _T("float4 BlendWeight :BLENDWEIGHT;\n");
		}

		if(pVBuffer->HaveBlendIndicesInfo())
		{
			TempDeclare += _T("float4 BlendIndices :BLENDINDICES;\n");
		}

		OutString += _T("struct VS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();

		if(pVBuffer->HavePositionInfo(0))
		{
			VSString VertexID = IntToString(0);		
			TempDeclare += _T("float3 Position") + VertexID + _T(":POSITION") + VertexID + _T(";\n");
		}

		for(unsigned int i = 0 ; i < pVBuffer->GetNormalLevel(); i++)
		{
			if(pVBuffer->HaveNormalInfo(i))
			{
				VSString NomalID = IntToString(i);
				TempDeclare += _T("float3 Normal") + NomalID + _T(":NORMAL") + NomalID + _T(";\n");
			}
		}

		if(pVBuffer->HaveTangentInfo())
		{
			TempDeclare += _T("float4 Tangent : TANGENT;\n");
			if (pVBuffer->HaveBinormalInfo())
			{
				TempDeclare += _T("float3 Binormal : BINORMAL;\n");
			}
			
		}

		if(pVBuffer->HaveBlendWeightInfo())
		{
			TempDeclare += _T("float4 BlendWeight :BLENDWEIGHT;\n");
		}

		if(pVBuffer->HaveBlendIndicesInfo())
		{
			TempDeclare += _T("float4 BlendIndices :BLENDINDICES;\n");
		}

		OutString += _T("struct VS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();

		if(pVBuffer->HavePositionInfo(0))
		{
			VSString VertexID = IntToString(0);		
			TempDeclare += _T("float3 Position") + VertexID + _T(":POSITION") + VertexID + _T(";\n");
		}

		if(pVBuffer->HaveBlendWeightInfo())
		{
			TempDeclare += _T("float4 BlendWeight :BLENDWEIGHT;\n");
		}

		if(pVBuffer->HaveBlendIndicesInfo())
		{
			TempDeclare += _T("float4 BlendIndices :BLENDINDICES;\n");
		}

		OutString += _T("struct VS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
}
void VSDX9Renderer::CreateVOutputDeclare(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										 VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		TempDeclare += _T("float4 Position:POSITION;\n");

		for(unsigned int i = 0 ; i < pVBuffer->GetColorLevel(); i++)
		{
			if(pVBuffer->HaveColorInfo(i))
			{
				VSString ColorID = IntToString(i);
				TempDeclare += _T("float4 Color") + ColorID + _T(":COLOR") + ColorID + _T(";\n");
			}
		}

		unsigned int j = 0;
		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{

			if(pVBuffer->HaveTexCoordInfo(i))
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float2 TexCoord") + TextureID+ _T(":TEXCOORD") + TextureID + _T(";\n");
				j++;
			}
		}
		unsigned int uiLightNum = MSPara1.LightArray.GetNum();
		if(uiLightNum)
		{
			
			if(pVBuffer->HaveNormalInfo(0))
			{		
				VSString TextureID = IntToString(j);

				TempDeclare += _T("float3 Normal:TEXCOORD") + TextureID + _T(";\n");
				j++;
			}




			if(pVBuffer->HaveTangentInfo())
			{
				{
					VSString TextureID = IntToString(j);

					TempDeclare += _T("float3 Tangent:TEXCOORD") + TextureID + _T(";\n");
					j++;
				}
				{
					VSString TextureID = IntToString(j);

					TempDeclare += _T("float3 Binormal:TEXCOORD") + TextureID + _T(";\n");
					j++;
				}
			}

			VSString TextureID  = IntToString(j);
			TempDeclare += _T("float3 Pos:TEXCOORD") + TextureID + _T(";\n");
			j++;


			
		}
		OutString += _T("struct VS_OUTPUT \n{\n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		TempDeclare += _T("float4 Position:POSITION;\n");

		unsigned int j = 0;
		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{

			if(pVBuffer->HaveTexCoordInfo(i))
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float2 TexCoord") + TextureID+ _T(":TEXCOORD") + TextureID + _T(";\n");
				j++;
			}

		}

		VSString TextureID = IntToString(j);
		TempDeclare += _T("float ViewZ:TEXCOORD") + TextureID + _T(";\n");
		j++;
		if(pVBuffer->HaveNormalInfo(0))
		{		
			VSString TextureID = IntToString(j);

			TempDeclare += _T("float3 Normal:TEXCOORD") + TextureID + _T(";\n");
			j++;
		}




		if(pVBuffer->HaveTangentInfo())
		{
			{
				VSString TextureID = IntToString(j);

				TempDeclare += _T("float3 Tangent:TEXCOORD") + TextureID + _T(";\n");
				j++;
			}
			{
				VSString TextureID = IntToString(j);

				TempDeclare += _T("float3 Binormal:TEXCOORD") + TextureID + _T(";\n");
				j++;
			}
		}

		OutString += _T("struct VS_OUTPUT \n{\n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		TempDeclare += _T("float4 Position:POSITION;\n");

		VSString TextureID = IntToString(0);
		TempDeclare += _T("float ViewZ:TEXCOORD") + TextureID + _T(";\n");

		OutString += _T("struct VS_OUTPUT \n{\n") + TempDeclare + _T("};\n");
	}
}
void VSDX9Renderer::CreateVUserConstant(VSVShader * pVShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		unsigned int uiRegisterID = 0;

		VSString RegisterID = IntToString(uiRegisterID);
		OutString += _T("row_major float4x4 WorldViewProjectMatrix : register(c") + RegisterID + _T(");\n");
		VSShaderStringFactory::ms_WorldViewProjectMatrix = _T("WorldViewProjectMatrix");
		VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_WorldViewProjectMatrix,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
		pVShader->m_pUserConstant.AddElement(pUserConstant);
		uiRegisterID += pUserConstant->GetRegisterNum();


		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		if(MSPara1.pGeometry->GetAffectBoneNum() && pVBuffer->GetBlendWeightDate() && pVBuffer->GetBlendIndicesDate() )
		{
			
			RegisterID = IntToString(uiRegisterID);
			unsigned int uiBoneNum = VSResourceManager::GetGpuSkinBoneNum();
			VSString BoneNum = IntToString(uiBoneNum * 3);
			OutString += _T("float4 BoneVector[") + BoneNum + _T("]") + _T(" : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_BoneMatrix = _T("BoneVector");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_BoneMatrix,NULL,sizeof(VSREAL) * 4 * uiBoneNum * 3,uiRegisterID,uiBoneNum * 3);
			pVShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH || uiPassType == VSPass::PT_PREZ)
	{
		unsigned int uiRegisterID = 0;
		{
			VSString RegisterID = IntToString(uiRegisterID);
			OutString += _T("row_major float4x4 WorldViewProjectMatrix : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_WorldViewProjectMatrix = _T("WorldViewProjectMatrix");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_WorldViewProjectMatrix,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
			pVShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}


		{
			VSString RegisterID = IntToString(uiRegisterID);
			OutString += _T("row_major float4x4 WorldViewMatrix : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_WorldViewMatrix = _T("WorldViewMatrix");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_WorldViewMatrix,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
			pVShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}

		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		if(MSPara1.pGeometry->GetAffectBoneNum() && pVBuffer->GetBlendWeightDate() && pVBuffer->GetBlendIndicesDate() )
		{
			VSString RegisterID  = IntToString(uiRegisterID);
			unsigned int uiBoneNum = VSResourceManager::GetGpuSkinBoneNum();
			VSString BoneNum = IntToString(uiBoneNum * 3);
			OutString += _T("float4 BoneVector[") + BoneNum + _T("]") + _T(" : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_BoneMatrix = _T("BoneVector");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_BoneMatrix,NULL,sizeof(VSREAL) * 4 * uiBoneNum * 3, uiRegisterID,uiBoneNum * 3);
			pVShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}
	}


}
void VSDX9Renderer::CreateVFuntion(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
								   VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		VSString FuntionBody;	
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		FuntionBody +=_T("float3 Position;\n");
		unsigned int uiLightNum = MSPara1.LightArray.GetNum();
		if(uiLightNum)
		{

			if(pVBuffer->HaveNormalInfo(0))
			{
				FuntionBody +=_T("float3 Normal;\n");
			}

			if(pVBuffer->HaveTangentInfo())
			{
				FuntionBody +=_T("float4 Tangent;\n");
				FuntionBody +=_T("float3 Binormal;\n");
			}
			
		}

		if(MSPara1.pGeometry->GetAffectBoneNum() && pVBuffer->HaveBlendWeightInfo() && pVBuffer->HaveBlendIndicesInfo())
		{
			FuntionBody +=_T("float4 U = 0;\n");
			FuntionBody +=_T("float4 V = 0;\n");
			FuntionBody +=_T("float4 N = 0;\n");

			VSRenderer::ms_pRenderer->ComputeBoneVector("Input.BlendIndices","Input.BlendWeight",VSShaderStringFactory::ms_BoneMatrix
				,"U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");

			FuntionBody += "Position = ";
			VSRenderer::ms_pRenderer->BoneTranPos("float4(Input.Position0,1)","U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");

			if(uiLightNum)
			{

				if(pVBuffer->HaveNormalInfo(0))
				{
					FuntionBody += "Normal = Input.Normal0;\n";
					if (pVBuffer->NormalDateType(0) == VSDataBuffer::DT_UBYTE4N)
					{
						FuntionBody += "Normal = ";
						VSRenderer::ms_pRenderer->DecodeNormal1("Normal",FuntionBody);
						FuntionBody +=_T(";\n");
					}

					
				}	

				if(pVBuffer->HaveTangentInfo())
				{
					FuntionBody += "Tangent = Input.Tangent;\n";
					if (pVBuffer->TangentDateType() == VSDataBuffer::DT_UBYTE4N)
					{
						FuntionBody += "Tangent = ";
						VSRenderer::ms_pRenderer->DecodeNormal1("Tangent",FuntionBody);
						FuntionBody +=_T(";\n");
					}
					if(pVBuffer->HaveBinormalInfo())
					{
						FuntionBody += "Binormal = Input.Binormal;\n";
						if (pVBuffer->BinormalDateType() == VSDataBuffer::DT_UBYTE4N)
						{
							FuntionBody += "Binormal = ";
							VSRenderer::ms_pRenderer->DecodeNormal1("Binormal",FuntionBody);
							FuntionBody +=_T(";\n");
						}

					}
					else
					{
						FuntionBody += "Binormal = ";
						VSRenderer::ms_pRenderer->DecodeNormal2(_T("Normal"),_T("Tangent"),FuntionBody);
						FuntionBody +=_T(";\n");
					}
					
				}

				

				if(pVBuffer->HaveNormalInfo(0))
				{
					FuntionBody += "Normal = ";
					VSRenderer::ms_pRenderer->BoneTranNormal("Normal","U","V","N",FuntionBody);
					FuntionBody +=_T(";\n");
				}

				if(pVBuffer->HaveTangentInfo())
				{
					FuntionBody += "Tangent.xyz = ";
					VSRenderer::ms_pRenderer->BoneTranNormal("Tangent.xyz","U","V","N",FuntionBody);
					FuntionBody +=_T(";\n");

					FuntionBody += "Binormal = ";
					VSRenderer::ms_pRenderer->BoneTranNormal("Binormal","U","V","N",FuntionBody);
					FuntionBody +=_T(";\n");
				}
			}		
		}
		else
		{	
			FuntionBody +=_T("Position = Input.Position0;\n");
			if(uiLightNum)
			{
				
				if(pVBuffer->HaveNormalInfo(0))
				{	
					FuntionBody +=_T("Normal = Input.Normal0;\n");
					if (pVBuffer->NormalDateType(0) == VSDataBuffer::DT_UBYTE4N)
					{
						FuntionBody += "Normal = ";
						VSRenderer::ms_pRenderer->DecodeNormal1("Normal",FuntionBody);
						FuntionBody +=_T(";\n");
					}
				}

				

				if(pVBuffer->HaveTangentInfo())
				{
					FuntionBody +=_T("Tangent = Input.Tangent;\n");
					if (pVBuffer->TangentDateType() == VSDataBuffer::DT_UBYTE4N)
					{
						FuntionBody += "Tangent = ";
						VSRenderer::ms_pRenderer->DecodeNormal1("Tangent",FuntionBody);
						FuntionBody +=_T(";\n");
					}
					if(pVBuffer->HaveBinormalInfo())
					{
						FuntionBody +=_T("Binormal = Input.Binormal;\n");
					}
					else
					{
						FuntionBody += "Binormal = ";
						VSRenderer::ms_pRenderer->DecodeNormal2(_T("Normal"),_T("Tangent"),FuntionBody);
						FuntionBody +=_T(";\n");
					}
				}
				
				

				
			}	
		}
		FuntionBody += _T("Out.Position = mul(float4(Position,1), ") +
					VSShaderStringFactory::ms_WorldViewProjectMatrix + _T(");\n");

		for(unsigned int i = 0 ; i < pVBuffer->GetColorLevel(); i++)
		{

			if(pVBuffer->HaveColorInfo(i))
			{
				VSString ColorID = IntToString(i);
				FuntionBody += _T("Out.Color") + ColorID + _T(" = Input.Color") + ColorID + _T(";\n");
			}
		}

		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{
			if(pVBuffer->HaveTexCoordInfo(0))
			{
				VSString TextureID = IntToString(i);
				FuntionBody += _T("Out.TexCoord") + TextureID + _T(" = Input.TexCoord") + TextureID + _T(";\n");
			}
		}
		if(uiLightNum)
		{
			
			if(pVBuffer->HaveNormalInfo(0))
			{	
				FuntionBody +=_T("Out.Normal = Normal;\n");

			}


			if(pVBuffer->HaveTangentInfo())
			{
				FuntionBody +=_T("Out.Tangent = Tangent.xyz;\n");
				FuntionBody +=_T("Out.Binormal = Binormal;\n");
			}



			FuntionBody +=_T("Out.Pos = Position;\n");	


			
		}
		OutString += _T("VS_OUTPUT ") + ms_VShaderProgramMain + _T("( VS_INPUT Input)\n{\nVS_OUTPUT Out = (VS_OUTPUT) 0; \n") + FuntionBody + _T("return Out;\n};\n");	
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSString FuntionBody;	
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		FuntionBody +=_T("float3 Position;\n");

		if(pVBuffer->HaveNormalInfo(0))
		{
			FuntionBody +=_T("float3 Normal;\n");
		}

		if(pVBuffer->HaveTangentInfo())
		{
			FuntionBody +=_T("float4 Tangent;\n");
			FuntionBody +=_T("float3 Binormal;\n");
		}

		if(MSPara1.pGeometry->GetAffectBoneNum() && pVBuffer->HaveBlendWeightInfo() && pVBuffer->HaveBlendIndicesInfo())
		{
			FuntionBody +=_T("float4 U = 0;\n");
			FuntionBody +=_T("float4 V = 0;\n");
			FuntionBody +=_T("float4 N = 0;\n");

			VSRenderer::ms_pRenderer->ComputeBoneVector("Input.BlendIndices","Input.BlendWeight",VSShaderStringFactory::ms_BoneMatrix
				,"U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");

			FuntionBody += "Position = ";
			VSRenderer::ms_pRenderer->BoneTranPos("float4(Input.Position0,1)","U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");

			if(pVBuffer->HaveNormalInfo(0))
			{
				FuntionBody += "Normal = Input.Normal0;\n";
				if (pVBuffer->NormalDateType(0) == VSDataBuffer::DT_UBYTE4N)
				{
					FuntionBody += "Normal = ";
					VSRenderer::ms_pRenderer->DecodeNormal1("Normal",FuntionBody);
					FuntionBody +=_T(";\n");
				}


			}	

			if(pVBuffer->HaveTangentInfo())
			{
				FuntionBody += "Tangent = Input.Tangent;\n";
				if (pVBuffer->TangentDateType() == VSDataBuffer::DT_UBYTE4N)
				{
					FuntionBody += "Tangent = ";
					VSRenderer::ms_pRenderer->DecodeNormal1("Tangent",FuntionBody);
					FuntionBody +=_T(";\n");
				}
				if(pVBuffer->HaveBinormalInfo())
				{
					FuntionBody += "Binormal = Input.Binormal;\n";
					if (pVBuffer->BinormalDateType() == VSDataBuffer::DT_UBYTE4N)
					{
						FuntionBody += "Binormal = ";
						VSRenderer::ms_pRenderer->DecodeNormal1("Binormal",FuntionBody);
						FuntionBody +=_T(";\n");
					}

				}
				else
				{
					FuntionBody += "Binormal = ";
					VSRenderer::ms_pRenderer->DecodeNormal2(_T("Normal"),_T("Tangent"),FuntionBody);
					FuntionBody +=_T(";\n");
				}

			}

			

			if(pVBuffer->HaveNormalInfo(0))
			{
				FuntionBody += "Normal = ";
				VSRenderer::ms_pRenderer->BoneTranNormal("Normal","U","V","N",FuntionBody);
				FuntionBody +=_T(";\n");
			}

			if(pVBuffer->HaveTangentInfo())
			{
				FuntionBody += "Tangent.xyz = ";
				VSRenderer::ms_pRenderer->BoneTranNormal("Tangent.xyz","U","V","N",FuntionBody);
				FuntionBody +=_T(";\n");

				FuntionBody += "Binormal = ";
				VSRenderer::ms_pRenderer->BoneTranNormal("Binormal","U","V","N",FuntionBody);
				FuntionBody +=_T(";\n");
			}
		}
		else
		{	
			FuntionBody +=_T("Position = Input.Position0;\n");
			if(pVBuffer->HaveNormalInfo(0))
			{	
				FuntionBody +=_T("Normal = Input.Normal0;\n");
				if (pVBuffer->NormalDateType(0) == VSDataBuffer::DT_UBYTE4N)
				{
					FuntionBody += "Normal = ";
					VSRenderer::ms_pRenderer->DecodeNormal1("Normal",FuntionBody);
					FuntionBody +=_T(";\n");
				}
			}



			if(pVBuffer->HaveTangentInfo())
			{
				FuntionBody +=_T("Tangent = Input.Tangent;\n");
				if (pVBuffer->TangentDateType() == VSDataBuffer::DT_UBYTE4N)
				{
					FuntionBody += "Tangent = ";
					VSRenderer::ms_pRenderer->DecodeNormal1("Tangent",FuntionBody);
					FuntionBody +=_T(";\n");
				}
				if(pVBuffer->HaveBinormalInfo())
				{
					FuntionBody +=_T("Binormal = Input.Binormal;\n");
				}
				else
				{
					FuntionBody += "Binormal = ";
					VSRenderer::ms_pRenderer->DecodeNormal2(_T("Normal"),_T("Tangent"),FuntionBody);
					FuntionBody +=_T(";\n");
				}
			}

			
		}
		FuntionBody += _T("Out.Position = mul(float4(Position,1), ") +
			VSShaderStringFactory::ms_WorldViewProjectMatrix + _T(");\n");


		if(pVBuffer->HaveNormalInfo(0))
		{	
			FuntionBody +=_T("Out.Normal = Normal;\n");

		}

		if(pVBuffer->HaveTangentInfo())
		{
			FuntionBody +=_T("Out.Tangent = Tangent.xyz;\n");
			FuntionBody +=_T("Out.Binormal = Binormal;\n");
		}
		FuntionBody += _T("Out.ViewZ = mul(float4(Position,1), ") +
			VSShaderStringFactory::ms_WorldViewMatrix + _T(").z;\n");
		OutString += _T("VS_OUTPUT ") + ms_VShaderProgramMain + _T("( VS_INPUT Input)\n{\nVS_OUTPUT Out = (VS_OUTPUT) 0; \n") + FuntionBody + _T("return Out;\n};\n");	
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		VSString FuntionBody;	
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		FuntionBody +=_T("float3 Position;\n");

		if(MSPara1.pGeometry->GetAffectBoneNum() && pVBuffer->HaveBlendWeightInfo() && pVBuffer->HaveBlendIndicesInfo())
		{
			FuntionBody +=_T("float4 U = 0;\n");
			FuntionBody +=_T("float4 V = 0;\n");
			FuntionBody +=_T("float4 N = 0;\n");

			VSRenderer::ms_pRenderer->ComputeBoneVector("Input.BlendIndices","Input.BlendWeight",VSShaderStringFactory::ms_BoneMatrix
				,"U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");

			FuntionBody += "Position = ";
			VSRenderer::ms_pRenderer->BoneTranPos("float4(Input.Position0,1)","U","V","N",FuntionBody);
			FuntionBody +=_T(";\n");
		}
		else
		{	
			FuntionBody +=_T("Position = Input.Position0;\n");
		}
		FuntionBody += _T("Out.Position = mul(float4(Position,1), ") +
			VSShaderStringFactory::ms_WorldViewProjectMatrix + _T(");\n");


		FuntionBody += _T("Out.ViewZ = mul(float4(Position,1), ") +
			VSShaderStringFactory::ms_WorldViewMatrix + _T(").z;\n");
		OutString += _T("VS_OUTPUT ") + ms_VShaderProgramMain + _T("( VS_INPUT Input)\n{\nVS_OUTPUT Out = (VS_OUTPUT) 0; \n") + FuntionBody + _T("return Out;\n};\n");	
	}
}
void VSDX9Renderer::CreatePInputDeclare(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		for(unsigned int i = 0 ; i < pVBuffer->GetColorLevel(); i++)
		{

			if(pVBuffer->HaveColorInfo(i))
			{
				VSString ColorID = IntToString(i);
				TempDeclare += _T("float4 Color") + ColorID + _T(":COLOR") + ColorID + _T(";\n");
				VSShaderStringFactory::ms_PSColor[i] = _T("ps_Input.Color") + ColorID;
			}
		}
		unsigned int j = 0;
		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{
			if(pVBuffer->HaveTexCoordInfo(i))
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float2 TexCoord") + TextureID+ _T(":TEXCOORD") + TextureID + _T(";\n");
				VSShaderStringFactory::ms_PSTextureInputCoordValue[i] = _T("ps_Input.TexCoord") + TextureID;
				j++;
			}
		}
		unsigned int uiLightNum = MSPara1.LightArray.GetNum();
		if(uiLightNum)
		{
			
			if(pVBuffer->HaveNormalInfo(0))
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float3 Normal:TEXCOORD") + TextureID + _T(";\n");
				VSShaderStringFactory::ms_PSInputLocalNormal = _T("ps_Input.Normal");
				j++;
			}



			if(pVBuffer->HaveTangentInfo())
			{
				{
					VSString TextureID = IntToString(j);
					TempDeclare += _T("float3 Tangent:TEXCOORD") + TextureID + _T(";\n");
					VSShaderStringFactory::ms_PSInputLocalTangent = _T("ps_Input.Tangent");
					j++;
				}

				{
					VSString TextureID = IntToString(j);
					TempDeclare += _T("float3 Binormal:TEXCOORD") + TextureID + _T(";\n");
					VSShaderStringFactory::ms_PSInputLocalBinormal = _T("ps_Input.Binormal");
					j++;
				}
			}

			VSString TextureID = IntToString(j);
			TempDeclare += _T("float3 Pos:TEXCOORD") + TextureID + _T(";\n");
			j++;
			VSShaderStringFactory::ms_PSInputLocalPos = _T("ps_Input.Pos");


			
		}
		OutString += _T("struct PS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		unsigned int j = 0;

		for(unsigned int i = 0 ; i < pVBuffer->GetTexCoordLevel(); i++)
		{
			if(pVBuffer->HaveTexCoordInfo(i))
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float2 TexCoord") + TextureID+ _T(":TEXCOORD") + TextureID + _T(";\n");
				VSShaderStringFactory::ms_PSTextureInputCoordValue[i] = _T("ps_Input.TexCoord") + TextureID;
				j++;
			}
		}

		VSString TextureID = IntToString(j);
		TempDeclare += _T("float ViewZ:TEXCOORD") + TextureID + _T(";\n");
		VSShaderStringFactory::ms_PSInputViewZ = _T("ps_Input.ViewZ");
		j++;
		if(pVBuffer->HaveNormalInfo(0))
		{
			VSString TextureID = IntToString(j);
			TempDeclare += _T("float3 Normal:TEXCOORD") + TextureID + _T(";\n");
			VSShaderStringFactory::ms_PSInputLocalNormal = _T("ps_Input.Normal");
			j++;
		}



		if(pVBuffer->HaveTangentInfo())
		{
			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float3 Tangent:TEXCOORD") + TextureID + _T(";\n");
				VSShaderStringFactory::ms_PSInputLocalTangent = _T("ps_Input.Tangent");
				j++;
			}

			{
				VSString TextureID = IntToString(j);
				TempDeclare += _T("float3 Binormal:TEXCOORD") + TextureID + _T(";\n");
				VSShaderStringFactory::ms_PSInputLocalBinormal = _T("ps_Input.Binormal");
				j++;
			}
		}
		OutString += _T("struct PS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		VSString TempDeclare;
		VSVertexBuffer * pVBuffer = MSPara1.pGeometry->GetMeshDate()->GetVertexBuffer();
		unsigned int j = 0;
		VSString TextureID = IntToString(j);
		TempDeclare += _T("float ViewZ:TEXCOORD") + TextureID + _T(";\n");
		VSShaderStringFactory::ms_PSInputViewZ = _T("ps_Input.ViewZ");
		j++;
		OutString += _T("struct PS_INPUT \n{ \n") + TempDeclare + _T("};\n");
	}
}
void VSDX9Renderer::CreatePOutputDeclare(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										 VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL || uiPassType == VSPass::PT_NORMALDEPTH || uiPassType == VSPass::PT_PREZ)
	{
		VSString TempDeclare;
		TempDeclare += _T("float4 Color:COLOR;");
		VSShaderStringFactory::ms_PSTextureOutputColorValue = _T("Out.Color");
		OutString += _T("struct PS_OUTPUT \n{ \n") + TempDeclare + _T("\n};\n");
	}

}
void VSDX9Renderer::CreatePUserConstant(VSPShader* pPShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
										VSString & OutString)
{

	if (uiPassType == VSPass::PT_MATERIAL)
	{
		unsigned int uiRegisterID = 0;
		unsigned int uiLightNum = MSPara1.LightArray.GetNum();
		VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
		if (uiLightNum)
		{
		
			VSString RegisterID = IntToString(uiRegisterID);


			VSArray<VSLightPtr> & LightArray = MSPara1.LightArray;
			
			int iLightNum[VSLight::LT_MAX] = { 0 };	
			for (unsigned int i = 0 ; i < LightArray.GetNum() ; i++)
			{
				if (LightArray[i])
				{
					for (unsigned int j = 0 ; j < VSLight::LT_MAX ; j++)
					{
						if (LightArray[i]->GetLightType() == j)
						{

							iLightNum[j]++;
						}
					}
				}
			}
	// 		if (!iLightNum[VSLight::LT_PROJ])
	// 		{
				{
					OutString += _T("row_major float4x4 WorldMatrix : register(c") + RegisterID + _T(");\n");
					VSShaderStringFactory::ms_WorldMatrix = _T("WorldMatrix"); 
					VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_WorldMatrix,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
					pPShader->m_pUserConstant.AddElement(pUserConstant);
					uiRegisterID += pUserConstant->GetRegisterNum();
					RegisterID = IntToString(uiRegisterID);
				}

				{
					OutString += _T("float4 CameraWorldPos : register(c") + RegisterID + _T(");\n");
					VSShaderStringFactory::ms_CameraWorldPos = _T("CameraWorldPos"); 
					VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_CameraWorldPos,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
					pPShader->m_pUserConstant.AddElement(pUserConstant);
					uiRegisterID += pUserConstant->GetRegisterNum();
					RegisterID = IntToString(uiRegisterID);
				}
			/*}*/

			for (unsigned int i = 0 ; i < VSLight::LT_MAX ; i++)
			{
				if (!iLightNum[i])
				{
					continue;
				}
				VSString TypeString;
				GetLightType(i,TypeString);
				if (i == VSLight::LT_DIRECTION)
				{		
					OutString += TypeString + _T(" DirLight[") + IntToString(iLightNum[i]) + _T("] : register(c") + RegisterID + _T(");\n");
					VSShaderStringFactory::ms_LightName[i] = _T("DirLight");
					VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_LightName[i],NULL,sizeof(VSREAL) * 12 * iLightNum[i],uiRegisterID,3 * iLightNum[i]);
					pPShader->m_pUserConstant.AddElement(pUserConstant);
					uiRegisterID += pUserConstant->GetRegisterNum();
					RegisterID = IntToString(uiRegisterID);
				}
				else if (i == VSLight::LT_POINT)
				{
					OutString += TypeString + _T(" PointLight[") + IntToString(iLightNum[i]) + _T("] : register(c") + RegisterID + _T(");\n");
					VSShaderStringFactory::ms_LightName[i] = _T("PointLight");
					VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_LightName[i],NULL,sizeof(VSREAL) * 16 * iLightNum[i],uiRegisterID,4 * iLightNum[i]);
					pPShader->m_pUserConstant.AddElement(pUserConstant);
					uiRegisterID += pUserConstant->GetRegisterNum();
					RegisterID = IntToString(uiRegisterID);
				}
				else if (i == VSLight::LT_SPOT)
				{
					OutString += TypeString + _T(" SpotLight[") + IntToString(iLightNum[i]) + _T("] : register(c") + RegisterID + _T(");\n");
					VSShaderStringFactory::ms_LightName[i] = _T("SpotLight");
					VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_LightName[i],NULL,sizeof(VSREAL) * 24 * iLightNum[i],uiRegisterID,6 * iLightNum[i]);
					pPShader->m_pUserConstant.AddElement(pUserConstant);
					uiRegisterID += pUserConstant->GetRegisterNum();
					RegisterID = IntToString(uiRegisterID);
				}
			}
		}
		pMaterial->CreateConstValueDeclare(OutString,uiRegisterID);
		pMaterial->CreateCustomValue(pPShader);

		unsigned uiTexRegisterID = 0;
		if (pMaterial->GetShaderMainFunction()->GetSMType() == VSShaderMainFunction::SM_OREN_NAYAR)
		{
			VSOrenNayarShaderFunction * pOrenNayarShaderFunction = (VSOrenNayarShaderFunction *)pMaterial->GetShaderMainFunction();
			if (pOrenNayarShaderFunction->UseLookUpTable())
			{
				OutString +=VSRenderer::ms_pRenderer->Sampler(VSTexture::TT_2D) + _T(" ");
				VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler = _T("OrenNayarLookUpTableSampler"); 
				OutString += VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler
					+ VSRenderer::ms_pRenderer->SetRegister(VSRenderer::RT_S,uiTexRegisterID) +_T(";\n");
				

				VSUserSampler * pSampler = VS_NEW VSUserSampler(VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler,VSTexture::TT_2D,uiTexRegisterID);
				pPShader->m_pUserSampler.AddElement(pSampler);
				VSTexAllState * pTex = (VSTexAllState *)VSTexAllState::GetOrenNayarLookUpTable();
				pPShader->SetParam(VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler,pTex);
				uiTexRegisterID++;
			}
		}
		pMaterial->CreateTextureDeclare(OutString,uiTexRegisterID);
		pMaterial->CreateCustomTexture(pPShader);
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
		unsigned int uiRegisterID = 0;
		{
			VSString RegisterID = IntToString(uiRegisterID);
			OutString += _T("row_major float4x4 WorldMatrix : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_WorldMatrix = _T("WorldMatrix"); 
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_WorldMatrix,NULL,sizeof(VSREAL) * 16,uiRegisterID,4);
			pPShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}
		{
			VSString RegisterID = IntToString(uiRegisterID);
			OutString += _T("float FarZ : register(c") + RegisterID + _T(");\n");
			VSShaderStringFactory::ms_FarZ = _T("FarZ");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_FarZ,NULL,sizeof(VSREAL) * 1,uiRegisterID,1);
			pPShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();

			pMaterial->CreateConstValueDeclare(OutString,uiRegisterID);
			pMaterial->CreateCustomValue(pPShader);

			unsigned uiTexRegisterID = 0;
			pMaterial->CreateTextureDeclare(OutString,uiTexRegisterID);
			pMaterial->CreateCustomTexture(pPShader);
		}
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		unsigned int uiRegisterID = 0;
		{
			VSString RegisterID = IntToString(uiRegisterID);
			OutString = _T("float FarZ : register(c0);\n");
			VSShaderStringFactory::ms_FarZ = _T("FarZ");
			VSUserConstant * pUserConstant = VS_NEW VSUserConstant(VSShaderStringFactory::ms_FarZ,NULL,sizeof(VSREAL) * 1,uiRegisterID,1);
			pPShader->m_pUserConstant.AddElement(pUserConstant);
			uiRegisterID += pUserConstant->GetRegisterNum();
		}
	}
}
void VSDX9Renderer::CreatePFuntion(MaterialShaderPara1 &MSPara1,unsigned int uiPassType,
								   VSString & OutString)
{
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		VSString FuntionBody;
		VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
		pMaterial->GetShaderTreeString(FuntionBody,MSPara1.LightArray,VSShaderMainFunction::OST_FINAL,MSPara1.uiPassId);

		OutString += _T("PS_OUTPUT ") + ms_PShaderProgramMain + _T("(PS_INPUT ps_Input)\n{\nPS_OUTPUT Out = (PS_OUTPUT) 0;\n") + FuntionBody + _T("return Out;\n};\n");	
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
		VSString FuntionBody;
		VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
		pMaterial->GetShaderTreeString(FuntionBody,MSPara1.LightArray,VSShaderMainFunction::OST_NORMAL,MSPara1.uiPassId);

		OutString += _T("PS_OUTPUT ") + ms_PShaderProgramMain + _T("(PS_INPUT ps_Input)\n{\nPS_OUTPUT Out = (PS_OUTPUT) 0;\n") + FuntionBody + _T("return Out;\n};\n");	
	}
	else if (uiPassType == VSPass::PT_PREZ)
	{
		OutString = _T("PS_OUTPUT ") + ms_PShaderProgramMain + _T("(PS_INPUT ps_Input)\n{\n PS_OUTPUT Out = (PS_OUTPUT) 0;\nOut.Color = float4(") + VSShaderStringFactory::ms_PSInputViewZ + _T("/") + VSShaderStringFactory::ms_FarZ + _T(",0.0f,0.0f,1.0f);\nreturn Out;\n};");
	}
}

void VSDX9Renderer::DirectionalLight(int iLightNum,const VSString &Diffuse,const VSString &Specular ,const VSString &SpecularPow,
							  const VSString &WorldNormal,const VSString &WorldCameraDir,VSString & OutString)const
{
	for (int i = 0 ; i < iLightNum ; i++)
	{
		VSString ID = IntToString(i);
		OutString += _T(" + DirectionalLightFun(") + Diffuse + _T(",") + Specular + _T(",") + SpecularPow + _T(",") 
					+ WorldNormal + _T(",") + WorldCameraDir + _T(",") 
					+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightDiffuse,") 
					+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightSpecular,") 
					+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightWorldDirection)") ;
	}
}
void VSDX9Renderer::OrenNayarDirectionalLight(int iLightNum,const VSString &Diffuse,const VSString &Specular ,const VSString &SpecularPow,
									 const VSString &WorldNormal,const VSString &WorldCameraDir,bool bLookUpTable,VSString & OutString)const
{
	for (int i = 0 ; i < iLightNum ; i++)
	{
		VSString ID = IntToString(i);
		if (bLookUpTable == false)
		{
			OutString += _T(" + OrenNayarDirectionalLightFun(") + Diffuse + _T(",") + Specular + _T(",") + SpecularPow + _T(",") 
				+ WorldNormal + _T(",") + WorldCameraDir + _T(",") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightDiffuse,") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightSpecular,") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightWorldDirection)") ;
		}
		else
		{
			OutString += _T(" + OrenNayarDirectionalLightFun(") + Diffuse + _T(",") + Specular + _T(",") + SpecularPow + _T(",") 
				+ WorldNormal + _T(",") + WorldCameraDir + _T(",") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightDiffuse,") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightSpecular,") 
				+ VSShaderStringFactory::ms_LightName[VSLight::LT_DIRECTION] + _T("[") + ID + _T("].LightWorldDirection,")
				+ VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler + _T(")") ;
		}
		
	}
}
void VSDX9Renderer::PointLight(int iLightNum,const VSString &Diffuse,const VSString &Specular ,const VSString &SpecularPow,
							const VSString & WorldPos,const VSString &WorldNormal,const VSString &WorldCameraDir,
							VSString & OutString)const
{
	for (int i = 0 ; i < iLightNum ; i++)
	{
		VSString ID = IntToString(i);
		OutString += _T(" + PointLightFun(") + Diffuse + _T(",") + Specular + _T(",") + SpecularPow + _T(",") 
			+ WorldPos + _T(",") + WorldNormal + _T(",") + WorldCameraDir + _T(",") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightDiffuse,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightSpecular,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightAttenuation.x,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightAttenuation.y,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightAttenuation.z,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_POINT] + _T("[") + ID + _T("].LightWorldPos)") ;
	}
	
}
void VSDX9Renderer::SpotLight(int iLightNum,const VSString &Diffuse,const VSString &Specular ,const VSString &SpecularPow,
					   const VSString & WorldPos,const VSString &WorldNormal,const VSString &WorldCameraDir,
					   VSString & OutString)const
{
	for (int i = 0 ; i < iLightNum ; i++)
	{
		VSString ID = IntToString(i);
		OutString += _T(" + SpotLightFun(") + Diffuse + _T(",") + Specular + _T(",") + SpecularPow + _T(",") 
			+ WorldPos + _T(",") + WorldNormal + _T(",") + WorldCameraDir + _T(",") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightDiffuse,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightSpecular,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightAttenuation.x,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightAttenuation.y,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightAttenuation.z,") 

			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightParam.x,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightParam.y,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightParam.z,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightWorldPos,") 
			+ VSShaderStringFactory::ms_LightName[VSLight::LT_SPOT] + _T("[") + ID + _T("].LightWorldDirection)") ;

	}
}
void VSDX9Renderer::ProjLight(int iLightNum,const VSString &Diffuse,const VSString & WorldPos,const VSString &WorldNormal,
					   VSString & OutString)const
{
// 	OutString += _T("ProjLightFun(") + Diffuse + _T(",")
// 		+ WorldPos + _T(",") + WorldNormal + _T(",")
// 		+ VSUserConstant::ms_NameInShader[VSUserConstant::NIS_LIGHT_WORLD_DIRECTION] + _T(",")
// 		+ VSUserConstant::ms_NameInShader[VSUserConstant::NIS_LIGHT_VIEW_PROJECT_TEXTURE_MATRIX] + _T(",")
// 		+ VSShaderStringFactory::ms_PSProjLightSampler + _T(")");

}
void VSDX9Renderer::LocalToWorldPos(const VSString & LocalPos,VSString &OutString)const
{
	OutString += _T("TransPos(") + LocalPos + _T(",") + 
					VSShaderStringFactory::ms_WorldMatrix + _T(")");
}

void VSDX9Renderer::LocalToWorldNormal(const VSString & LocalNormal,VSString &OutString)const
{
	OutString += _T("TransNormal(") + LocalNormal + _T(",") + 
		VSShaderStringFactory::ms_WorldMatrix + _T(")");
}
void VSDX9Renderer::BumpNormal(const VSString &TexNormal,VSString &OutString)const
{
	OutString += _T("BumpNormal(") + VSShaderStringFactory::ms_PSInputLocalTangent + _T(",") 
				+ VSShaderStringFactory::ms_PSInputLocalBinormal + _T(",")
				+ VSShaderStringFactory::ms_PSInputLocalNormal + _T(",")
				+ TexNormal + _T(")");
}
void VSDX9Renderer::ComputeDir(const VSString & Orgin,const VSString & End,VSString &OutString)const
{
	OutString += _T("ComputeDir(") + Orgin + _T(",")
				+ End + _T(")");
}
void VSDX9Renderer::GetLightType(unsigned int i,VSString &OutString)const
{
	if (i == VSLight::LT_DIRECTION)
	{ 
		OutString = _T("DirLightType");
	}
	else if (i == VSLight::LT_POINT)
	{
		OutString = _T("PointLightType");
	}
	else if (i == VSLight::LT_SPOT)
	{
		OutString = _T("SpotLightType");
	}
}
void VSDX9Renderer::ComputeBoneVector(const VSString& BoneIndex, const VSString & BoneWeight,const VSString &BoneVector,
							  const  VSString & U,const VSString & V,const VSString &N,VSString &OutString)const
{
	OutString += _T("ComputeBoneVector(") + BoneIndex + _T(",")
		+ BoneWeight + _T(",") + BoneVector + _T(",") +  U + _T(",") + V + _T(",") + N  + _T(")");
}
void VSDX9Renderer::BoneTranPos( const VSString & LocalPos,const  VSString & U,const VSString & V,const VSString &N,VSString &OutString)const
{
	OutString += _T("TransPos(") + LocalPos + _T(",") +  U + _T(",") + V + _T(",") + N  + _T(")");
}
 void VSDX9Renderer::BoneTranNormal( const VSString & LocalNormal,const  VSString & U,const VSString & V,const VSString &N,VSString &OutString)const
{
	OutString += _T("TransNormal(") + LocalNormal + _T(",") +  U + _T(",") + V + _T(",") + N  + _T(")");
}
void VSDX9Renderer::DecodeNormal1(const VSString & Normal,VSString &OutString) const
{
	OutString += _T("DecodeNormal1(") + Normal + _T(")");
}
void VSDX9Renderer::RecodeNormal1(const VSString & Normal,VSString &OutString) const
{
	OutString += _T("RecodeNormal1(") + Normal + _T(")");
}
void VSDX9Renderer::DecodeNormal2(const VSString & Normal,const VSString & Tengent,VSString &OutString) const
{
	OutString += _T("DecodeNormal2(") + Normal + _T(",") + Tengent + _T(")");
}
void VSDX9Renderer::DecodeNormal3(const VSString & Normal,VSString &OutString) const
{
	OutString += _T("DecodeNormal3(") + Normal + _T(")");
}
void VSDX9Renderer::RecodeNormal3(const VSString & Normal,VSString &OutString) const
{
	OutString += _T("RecodeNormal3(") + Normal + _T(")");
}
void VSDX9Renderer::GreaterZeroPow(const VSString & Value,float Exp,VSString &OutString) const
{
	VSString SExp = RealToString(Exp);
	OutString += _T("GreaterZeroPow(") + Value + _T(",") + SExp + _T(")");
}
void VSDX9Renderer::SaturatePow(const VSString & Value,float Exp,VSString &OutString) const
{
	VSString SExp = RealToString(Exp);
	OutString += _T("SaturatePow(") + Value + _T(",") + SExp + _T(")");
}